package com.example.wendao.view.viewholder;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.RecyclerView;

import com.example.wendao.App;
import com.example.wendao.R;
import com.example.wendao.common.PageUtils;
import com.example.wendao.common.ViewHolderBind;
import com.example.wendao.model.entity.Forum;
import com.example.wendao.model.entity.User;
import com.example.wendao.viewmodel.UserViewModel;

public class TalkViewHolder extends RecyclerView.ViewHolder implements ViewHolderBind<Forum> {

    private TextView ForumTitle;
    private TextView ForumTime;
    private TextView ForumAgreeNum;
    private TextView ForumAuthor;
    private ConstraintLayout layout;
    private UserViewModel userViewModel;
    public TalkViewHolder(@NonNull ViewGroup viewGroup) {
        super(PageUtils.inflateItemView(viewGroup, R.layout.forum_item));
        ForumTitle = itemView.findViewById(R.id.forum_title_textview);
        ForumTime=itemView.findViewById(R.id.forum_time_textview);
        ForumAgreeNum=itemView.findViewById(R.id.forum_agree_textview);
        ForumAuthor=itemView.findViewById(R.id.forum_author_textview);
        layout=itemView.findViewById(R.id.forum_layout);
        userViewModel = new ViewModelProvider(App.getInstance()).get(UserViewModel.class);
    }

    @Override
    public void bindTo(Forum forum) {
        String title = forum.getForumTitle().length() < 10 ? forum.getForumTitle() : (forum.getForumTitle().substring(0,10) + "...");
        String time=forum.getForumTime();
        int agreenum=forum.getForumAgree();
        int authorid=forum.getForumAuthorId();
        User user=userViewModel.getUserById(authorid);
        String username=user.getUsername();
        ForumTitle.setText(title);
        ForumTime.setText(time);
        ForumAgreeNum.setText(String.valueOf(agreenum));
        ForumAuthor.setText(username);
        itemView.setOnClickListener(v->{
            Bundle bundle = new Bundle();
            bundle.putInt("forumid", forum.getForumId());
            bundle.putInt("authorid", authorid);
            Navigation.findNavController(v).navigate(R.id.fragment_talk_detail, bundle);
        });
    }
}
